package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.impl.CartDemoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartServiceImpl implements CartService{

    @Override
    public List<Cart> listCart(Cart cart) {
        List<Cart> list = new ArrayList<>();
        CartDao cartDao = new CartDemoImpl();

        try {
            list = cartDao.listCart(cart);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public int saveCart(String userId, Integer businessId, Integer foodId) {
        int i = 0;
        CartDao cartDao = new CartDemoImpl();

        try {
            i = cartDao.saveCart(userId,businessId,foodId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return i;
    }

    @Override
    public int updateCart(String userId, Integer businessId, Integer foodId, Integer quantity) {
        int i = 0;
        CartDao cartDao = new CartDemoImpl();
        try {
           i = cartDao.updateCart(userId, businessId, foodId, quantity);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return i;
    }

    @Override
    public int removeCart(String userId, Integer businessId, Integer foodId) {
        int i = 0;
        CartDao cartDao = new CartDemoImpl();
        try {
            i = cartDao.removeCart(userId, businessId, foodId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return i;
    }

}
